package com.example.deal.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;

import com.example.deal.broker.entity.Deal;

import feign.hystrix.FallbackFactory;

/**
 * FeignClientUserInterface的fallbackFactory类，该类需实现FallbackFactory接口，并覆写create方法 The
 * fallback factory must produce instances of fallback classes that implement
 * the interface annotated by {@link FeignClient}.
 */
@Component
public class FeignClientFallbackFactory implements FallbackFactory<FeignClientUserInterface> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

	@Override
	public FeignClientUserInterface create(Throwable cause) {
		return new FeignClientUserInterface() {
			@Override
			public Deal findById(Long id) {
				// 日志最好放在各个fallback方法中，而不要直接放在create方法中。
				// 否则在引用启动时，就会打印该日志。
				FeignClientFallbackFactory.LOGGER.info("fallback; reason was:", cause);
				Deal deal = new Deal();
				deal.setId(id.intValue());
				deal.setName("Dustyone");
				return deal;
			}
		};
	}
}